#include "circle_diagram.h"

namespace Packing_common
{
	void Circle_diagram::clear()
	{
		std::for_each(vgroups.begin(), vgroups.end(), std::mem_fun_ref(&std::vector<Vertex_handle>::clear));
		vgroups.clear();
		baseclass::clear();
	}
	void Circle_diagram::insert(const std::vector<Circle_union>& circle_unions)
	{
		clear();
		vgroups.resize(circle_unions.size());
		for (unsigned int i = 0; i < circle_unions.size(); i++)
		{
			vgroups[i].reserve(circle_unions[i].size());
			for (unsigned int j = 0; j < circle_unions[i].size(); j++)
			{
				Vertex_handle vh = baseclass::insert(Weighted_point(circle_unions[i].circle_at(j).center(), 0.0/*circle_unions[i].circle_at(j).squared_radius()*/));
				//assert(!vh->is_hidden());
				//if (!vh->is_hidden())
				//{
					vh->group_id = i;
					vgroups[i].push_back(vh);
				//}

			}
		}
	}

	void Circle_diagram::neighbor_pairs(std::vector< std::pair<unsigned int, unsigned int> >& proximal_pairs)
	{
		std::set< std::pair<unsigned int, unsigned int> > inserted;
		for (unsigned int i = 0; i < vgroups.size(); i++)
		{
			for (unsigned int j = 0; j < vgroups[i].size(); j++)
			{
				//if (vgroups[i][j]->is_hidden()) continue;
				Vertex_circulator vc = incident_vertices(vgroups[i][j]);
				Vertex_circulator vc_end = vc;
				do
				{
					//if (vc->group_id >= 9)
					//{
					//	std::cout<<vc->group_id<<std::endl;
						//system("pause");
					//}
					std::pair<unsigned int, unsigned int> np = std::make_pair(vgroups[i][j]->group_id, vc->group_id);
					if (!is_infinite(vc) && vgroups[i][j]->group_id < vc->group_id && inserted.find(np) == inserted.end())
					{
						proximal_pairs.push_back(np);
						inserted.insert(np);
					}
					++vc;
				} while (vc != vc_end);
			}
		}
	}
}